package com.workpaper.app.db.mybatis;

import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.apache.ibatis.type.TypeAliasRegistry;

import com.workpaper.app.cache.config.ConfigCache;
import com.workpaper.app.consts.dbconfig.DataSourceConst;
import com.workpaper.app.plan.entity.DailyPlan;
import com.workpaper.app.plan.mapper.DailyPlanMapper;
import com.workpaper.app.user.entity.User;
import com.workpaper.app.user.mapper.UserMapper;
import com.workpaper.app.workpaper.daily.entity.DailyWorkpaper;
import com.workpaper.app.workpaper.daily.mapper.DailyWorkpaperMapper;

public final class MyDB {
	
	private static SqlSessionFactory factory = null;
	
	private static List<SqlSession> openedSession = new ArrayList<SqlSession>();
	
	public static void initDB() {
		String driver = ConfigCache.get(DataSourceConst.driverClass);
		String jdbcURL = ConfigCache.get(DataSourceConst.jdbcURL);
		String username = ConfigCache.get(DataSourceConst.username);
		String password = ConfigCache.get(DataSourceConst.password);
		//非连接池数据源
		DataSource dataSource = new UnpooledDataSource(driver, jdbcURL, username, password);
		//事务工厂
		TransactionFactory transactionFactory = new JdbcTransactionFactory();
		//环境
		Environment environment = new Environment("development", transactionFactory, dataSource);
		//创建配置
		Configuration configuration = new Configuration(environment);
		//特性配置
		configuration.setLazyLoadingEnabled(true);	//启用懒加载
		configuration.setMapUnderscoreToCamelCase(true); //启用驼峰式规则映射属性
		
		//注册别名
		TypeAliasRegistry typeAliasRegistry = configuration.getTypeAliasRegistry();
		typeAliasRegistry.registerAlias("DailyPlan", DailyPlan.class);
		typeAliasRegistry.registerAlias("WorkpaperDaily", DailyWorkpaper.class);
		typeAliasRegistry.registerAlias("User", User.class);
//		<typeAlias type="com.workpaper.app.plan.entity.DailyPlan" alias="DailyPlan"/>
		
		//注册mapper
//		configuration.addMappers("com.workpaper.app.plan.mapper");
//		configuration.addMappers("dbcol.app.database.tableList.mapper");
		configuration.addMapper(DailyPlanMapper.class);	
		configuration.addMapper(DailyWorkpaperMapper.class);	
		configuration.addMapper(UserMapper.class);	
//		configuration.addMapper(BlogMapper.class);
		
		//创建Session工厂
		factory = new SqlSessionFactoryBuilder().build(configuration);
	}
	
	
	public static SqlSession openSession() {
		SqlSession sqlSession = factory.openSession();
		openedSession.add(sqlSession);
		return sqlSession ;
	}
	
	public static void clearResource() {
		openedSession.stream().forEach((session) -> session.close());
		openedSession.clear();
	}
	
	public static void coloseSession(SqlSession session) {
		session.commit();
		session.close();
	}
}
